package com.wing.storage.Controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.storage.model.entity.PalletBoxSkuIterm;
import com.wing.storage.model.request.PalletBoxSkuItermForm;
import com.wing.storage.model.response.PalletBoxSkuItermVo;
import com.wing.storage.service.PalletBoxSkuItermService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 料箱存货明细-Controller
 *
 */
@Validated
@Api(value = "料箱存货明细管理", tags = "#/storage/palletBoxSkuIterm")
@RestController("PalletBoxSkuItermController")
@RequestMapping("palletBoxSkuIterm/admin/palletBoxSkuIterm")
@RequiredArgsConstructor
public class PalletBoxSkuItermController {

    @Autowired
    private PalletBoxSkuItermService palletBoxSkuItermService;

    @ApiOperation(value = "料箱存货明细列表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<PalletBoxSkuItermVo> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            String skuCode,
            String skuName,
            String palletBoxCode,String shelfSpaceCode,
            Long shelfId,Long warehouseId,Long warehouseAreaId,Long warehouseShelfSpaceId) {



        Page<PalletBoxSkuItermVo> pg = new Page<>(page, limit);
        Map<String, Object> map = new HashMap();
        map.put("shelfSpaceCode", shelfSpaceCode);
        map.put("palletBoxCode", palletBoxCode);
        map.put("skuCode", skuCode);
        map.put("skuName", skuName);
        map.put("shelfId", shelfId);
        map.put("warehouseId", warehouseId);
        map.put("warehouseAreaId", warehouseAreaId);
        map.put("warehouseShelfSpaceId", warehouseShelfSpaceId);

        IPage<PalletBoxSkuItermVo> list = palletBoxSkuItermService.list4app(pg, map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "根据shelfSpaceCode查询对应的料箱", notes = "搜索")
    @GetMapping("/getPalletBoxList")
    public JsonResult getPalletBoxList(
            String shelfSpaceId,String skuCode ) {
        Map<String, Object> map = new HashMap();
        map.put("warehouseShelfSpaceId", shelfSpaceId);
        map.put("skuCode", skuCode);
        if(StringUtils.isBlank(shelfSpaceId)){
            return JsonResult.fail("请填写库位编码");
        }
        return JsonResult.success(palletBoxSkuItermService.getPalletBoxList(map));
    }

    @ApiOperation(value = "修改料箱存货明细", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(PalletBoxSkuItermForm.Update.class) PalletBoxSkuItermForm palletBoxSkuItermForm) {
        PalletBoxSkuIterm palletBoxSkuIterm = palletBoxSkuItermService.getById(palletBoxSkuItermForm.getId());
        if(palletBoxSkuIterm == null){
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
            .setEditable(PalletBoxSkuIterm.class)
            .setIgnoreError(true)
            .setIgnoreNullValue(true);
        BeanUtil.copyProperties(palletBoxSkuItermForm, palletBoxSkuIterm, copyOptions);
        if (palletBoxSkuItermService.updateById(palletBoxSkuIterm)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

}
